/*
 * Copyright 2008 [rapid-framework], Inc. All rights reserved.
 * Website: http://www.rapid-framework.org.cn
 */

    define("pages/jls_Wpth/form", [
            '../../pages/jls_Wpth/WpthForm.html.js',
        ],
        function(require){
            var _show=function(_ryjbxx,_wpth,findWpth){
                var tmp= require("../../pages/jls_Wpth/WpthForm.html");
                var htmlcompile = template.compile(tmp);
                var html = htmlcompile(
                    {
                        title:"Wpth",
                        jbxx:_ryjbxx,
                        data:_wpth,
                    });
                $("#gjcx").dialog('destroy')
                $("#dlgWpth").dialog('destroy')
                $("#wpthForm").html(html);
                $.parser.parse($("#wpthForm").parent());
                $('#dlgWpth').window('open');
                bindAction(findWpth);
            }

            var submitWpth=function(findWpth){
                var row = $('#jbxxId').datagrid('getSelected');
                var validate=$("#dlgWpth").form('validate');
                if (validate) {
                    var data =$("#fm").serialize()+"&jsbh="+row.jsbh+"&rybh="+row.rybh;
                    submitLoad();
                    $.ajax({
                        type : "POST",  //提交方式
                        url : "/jls_wpth/wpthAdd",//路径
                        data : data,//数据，这里使用的是Json格式进行传输
                        success : function(result) {
                            if (result.success) {
                                if($.isFunction(findWpth)){
                                    findWpth();
                                }
                                $.messager.alert("确定","保存成功","info");
                                javascript:$('#dlgWpth').dialog('close');
                                removeLoad();
                            }else{
                                $.messager.alert("确定","保存失败","info");
                                removeLoad();
                            }
                        },
                        error:function(){
                            removeLoad();

                        }
                    });
                }else{
                    $.messager.alert("提示", '必填项不能为空！');
                }
            };
            var updateWpth=function(findWpth){
                var row = $('#jbxxId').datagrid('getSelected');
                var validate=$("#dlgWpth").form('validate');
                if (validate) {
                    var data =$("#fm").serialize();
                    submitLoad();
                    $.ajax({
                        type : "POST",  //提交方式
                        url : "/jls_wpth/wpthUpdate",//路径
                        data : data,//数据，这里使用的是Json格式进行传输
                        success : function(result) {
                            if (result.success) {
                                $.messager.alert("确定","修改成功","info");
                                javascript:$('#dlgWpth').dialog('close');
                                removeLoad();
                                if($.isFunction(findWpth)){
                                    findWpth();
                                }
                            }else{
                                $.messager.alert("确定",result.msg,"info");
                                removeLoad();

                            }
                        },
                        error:function(){
                            removeLoad();

                        }
                    });
                }else{
                    $.messager.alert("提示", '必填项不能为空！');
                }
            };

            var bindAction=function(findWpth){
                $("#wpthSave").on("click",function(){
                    var wpthId = $("#wpthId").val();
                    if(wpthId=="" || wpthId==null){
                        submitWpth(findWpth);
                    }else{
                        updateWpth(findWpth);
                    }
                });
                $("#wpthCanel").on("click",function(){
                    $("#dlgWpth").dialog("close");
                });
            }
            return{
                show:_show
            }
        });
